<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" @click="deleteall"/>
    </label>
    <span>
      <span>已完成{{ successNum }}</span> / 全部{{ todoList.length }}
    </span>
    <button class="btn btn-danger" @click="deleteAllHandle">清除已完成任务</button>
  </div>
</template>

<script>
import _ from "lodash"
export default {
  name: "Footer",
  data() {
    return {
      todoList: [],
    };
  },

  methods:{
    deleteall(e){
      this.$bus.$emit("deleteall" , e.target.checked)
    },

    deleteAllHandle(){
      this.$bus.$emit("deleteevery")
    }
  },

  //初始化的时候给$bus绑定自定义事件获取todoList
  //暂时放在beforeMount中 可以让调用晚于绑定执行
  beforeMount() {
    this.$bus.$on("getTodo", (todoList) => {
      //深拷贝一个新的数组 以后List中的数组改变长度 当前组件显示的长度不会改变
      this.todoList = _.cloneDeep(todoList)
    });
  },

  computed: {
    //计算已完成的数量 累加done为true的
    successNum() {
      return this.todoList.reduce((p, c) => (c.done ? p + 1 : p), 0) 
    },
  },
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>